﻿// -----------------------------------------------------------------------
// <copyright file="Global.asax.cs" company="EPAM Systems">
////Copyright (c) EPAM Systems, 2012. All rights reserved.
// </copyright>
// -----------------------------------------------------------------------

using MBOP.Website.CustomAttributes;
using Ninject;

namespace MBOP.Website
{
	#region Usings

	using System.Web.Mvc;
	using System.Web.Routing;

	using MBOP.Configuration.ConfigElements;
	using MBOP.Configuration.ConfigExtensions;
	using MBOP.Website.CustomModelBinders;
	using MBOP.Website.Infrastructure;
	using MBOP.Website.ViewModels.Common;
	using MBOP.Website.ViewModels.Library;

	#endregion

	/// <summary>
	/// Defines the main application class
	/// </summary>
	public class MvcApplication : System.Web.HttpApplication
	{
		/// <summary>
		/// Handles the application start event.
		/// </summary>
		protected void Application_Start()
		{
			AreaRegistration.RegisterAllAreas();

			// WebApiConfig.Register(GlobalConfiguration.Configuration);
			FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
			RouteConfig.RegisterRoutes(RouteTable.Routes);

			DependencyResolver.SetResolver(new NinjectDependencyResolver());

			ModelBinders.Binders.Add(typeof(ImageViewModel), new ImageViewModelCustomBinder());
			ModelBinders.Binders.Add(typeof(BookSubscriptionViewModel), new BookSubscriptionViewModelCustomBinder());

		}

		/// <summary>
		/// Handles the session start event.
		/// </summary>
		protected void Session_Start()
		{
			AdminElementCollection adminCollection = AppConfigurationSection.GetInstance().Administrators;

			string personIdentity = string.Empty;

			if (this.Request.LogonUserIdentity != null)
			{
				personIdentity = this.Request.LogonUserIdentity.Name;
			}

			this.Session["isAdmin"] = adminCollection.IsConditionComplete(index => adminCollection[index].Token == personIdentity);
		}
	}
}